package CommonOperationsClientServer;

import java.io.*;
import java.util.Date;

public class FilesOperations {

    public static void sendFile(File f, PrintWriter output) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(f));
            byte[] array = new byte[5120];
            int arrayLength = bis.read(array);
            while (arrayLength > 0) {
                char[] arrayChar = new char[arrayLength];
                for (int i = 0; i < arrayLength; i++) {
                    arrayChar[i] = (char) array[i];
                }
                output.write(arrayChar);
                output.flush();
                arrayLength = bis.read(array);
            }
        } catch (Exception e) {
        } finally {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
    }

    public static void updateLog(String logFile, String userName, String host, String data) {
        File f = new File(logFile);
        if (f.exists() && f.isFile()) {
            if (f.length() >= 525000) {
                //@Abraham Este método devuelve algo importante…
                f.delete();
                createFile(logFile);
            }
            FileWriter fw = null;
            PrintWriter pw = null;
            try {
                Date date = new Date();
                String fileLog = "User: " + userName + "; Host: " + host + "; "
                        + date.toString() + "; " + data;
                fw = new FileWriter(f, true);
                pw = new PrintWriter(fw, true);
                pw.println(fileLog + "\n");
                pw.flush();
            } catch (Exception e) {
            } finally {
                OperationsRefactor.closeWriteFile(fw, pw);
            }
        }
    }

    public static void receiveFile(String directoryDestination, String fileName, BufferedReader input) {
        File file;
        BufferedOutputStream bos = null;
        try {
            file = new File(directoryDestination + "/" + fileName);
            bos = new BufferedOutputStream(new FileOutputStream(file));
            char[] array = new char[5120];
            int arrayLength = input.read(array);
            while (arrayLength == array.length) {
                for (int i = 0; i < arrayLength; i++) {
                    bos.write(array[i]);
                    bos.flush();
                }
                arrayLength = input.read(array);
            }
            for (int i = 0; i < arrayLength; i++) {
                bos.write(array[i]);
                bos.flush();
            }
        } catch (Exception e) {
        } finally {
            try {
                bos.close();
            } catch (Exception e) {
            }
        }
    }

    public static String getInfoFromLocalFile(String file) {
        String fileInfo = null;
        File f = new File(file);
        if (f.exists()) {
            Date date = new Date(f.lastModified());
            String type;
            if (f.isDirectory()) {
                type = "Directorio";
            } else {
                String name = f.getName();
                int contain = name.lastIndexOf(".");
                String format = name.substring(contain);
                type = "Fichero [" + format + "]";
            }
            String hidden;
            if (f.isHidden()) {
                hidden = "Oculto";
            } else {
                hidden = "Visible";
            }
            fileInfo = date.toString() + ";" + Long.toString(f.length()) + ";" + type + ";"
                    + create_String_permissions(f.canRead(), f.canWrite(), f.canExecute()) + ";" + hidden;
        }
        return fileInfo;
    }

    public static String create_String_permissions(boolean read, boolean write, boolean executable) {
        String permissions;
        if (read) {
            permissions = "r";
        } else {
            permissions = "-";
        }
        if (write) {
            permissions = permissions + "w";
        } else {
            permissions = permissions + "-";
        }
        if (executable) {
            permissions = permissions + "x";
        } else {
            permissions = permissions + "-";
        }
        return permissions;
    }

    public static void createFile(String fileName) {
        File file = new File(fileName);
        if (!file.exists() || !file.isFile()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
            }
        }
    }

    public static String executeCommand(String command) {
        Process proc;
        String commandResponse = "";
        String os = System.getProperty("os.name");
        if (os.contains("Windows")) {
            command = "cmd.exe /C" + command;
        }
        BufferedReader br = null;
        try {
            proc = Runtime.getRuntime().exec(command);
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                //@Abraham para este tipo de cosas es mejor emplear un StringBuilder
                commandResponse = commandResponse + line + "\n";
            }
            commandResponse = commandResponse + "-------------------------------"
                    + "--------------------------------------\n";
        } catch (Exception e) {
            commandResponse = "null";
        } finally {
            try {
                br.close();
            } catch (Exception e) {
            }
        }
        return commandResponse;
    }
}
